<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1 user-scalable=0">
    <title>用户登录</title>
    <link rel="stylesheet" href="/static/style.css">
    <link rel="stylesheet" href="/static/layui/css/layui.css">
    <style>
        form {
            width: 310px;
            margin: 0 auto;
        }
    </style>
</head>
<body>
<div class="login-page">
    <h1 class="login-title">投票登录</h1>
    <form class="layui-form layui-form-pane" >
        <div class="layui-form-item" pane>
            <label class="layui-form-label">账号</label>
            <div class="layui-input-block">
                <input name="jobNumber" lay-verType="tips" lay-verify="required" class="layui-input" type="text">
            </div>
        </div>
        <div class="layui-form-item" pane>
            <label class="layui-form-label">旧密码</label>
            <div class="layui-input-block">
                <input name="password" class="layui-input" lay-verType="tips" lay-verify="required|password" type="password">
            </div>
        </div>
        <div class="layui-form-item" pane>
            <label class="layui-form-label">新密码</label>
            <div class="layui-input-block">
                <input name="newPass" class="layui-input" lay-verType="tips" lay-verify="pass" type="password">
            </div>
        </div>
        <div class="layui-form-item" pane>
            <label class="layui-form-label">再次输入</label>
            <div class="layui-input-block">
                <input name="password1" class="layui-input" lay-verType="tips" lay-verify="pass1" type="password">
            </div>
        </div>
        <div class="login-submit">
            <button type="button" lay-submit lay-filter="login" class="layui-btn">修改</button>
        </div>
    </form>
</div>
<script src="/static/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/lib/model/'
    }).use(['jquery', 'layer', 'form'], function () {
        var $ = layui.$
            , layer = layui.layer
            , form = layui.form;

        //修改密码验证
        form.verify({
            pass: function (val, i) {
                if (!/[A-Za-z]+[0-9]+/.test(val))
                    return '新密码需为字母和数字的组合，且不含中文字符';
                if (val.length < 6 || val.length > 18) {
                    return '新密码需为6-18位'
                }
            }
            ,pass1: function (val, i) {
                if ($('#newPass').val() !== val) {
                    return '输入的两次密码不一致'
                }
            }
        });

        form.on('submit(login)', function (data) {
            let d = data.field;
            console.log(d);
            // 获取表单提交的信息
            // 提交表单信息
            $.ajax({
                type: 'POST'
                , url: '/changePass'
                , contentType: 'application/json'
                , data: JSON.stringify({jobNumber: d.jobNumber, password: d.password, newPass: d.newPass})
                , success: function (res) {
                    if (res.status == 200) {
                        layer.alert(res.msg, {
                            skin: 'layui-layer-lan'
                            , closeBtn: 0
                            , anim: 1 //动画类型
                        });
                    } else {
                        layer.alert(res.msg, {
                            skin: 'layui-layer-red'
                            , closeBtn: 0
                            , anim: 0 //动画类型
                        });
                    }
                }
            });

            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });


    });
</script>
</body>
</html>